﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuChi
{
    public class TM_PhieuChi_ViewModel : ViewModel<Accounting.Library.TM_PhieuChi>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
       
        public delegate void HangHoaIdChanged_EventHandler(object sender, HangHoaIdChangedEventArgs e);
        
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);

        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
      
        public event EventHandler LoadCompleted;
        public event EventHandler Saved;
        public event EventHandler Canceled;
        public event HangHoaIdChanged_EventHandler hangHoaIdChanged;
       
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;  
        #endregion

        #region Refresh Model
        public TM_PhieuChi_ViewModel()
        {
            BeginRefresh("NewTM_PhieuChi");
            
        }

        public TM_PhieuChi_ViewModel(string mode)
        {
            if (mode.Equals(BusinessConfiguration.Configuration.ChildWindow))
            {
                this.BeginRefresh("NewTM_PhieuChi");
            }
           
        }

        public TM_PhieuChi_ViewModel(int ChungTuThayTheId, int? LoaiChungTuThayTheId)
        {

            this.BeginRefresh("GetTM_PhieuChi", ChungTuThayTheId.ToString() + ";" + LoaiChungTuThayTheId.ToString());

        }

        public TM_PhieuChi_ViewModel(int Khoa)
        {
            BeginRefresh("GetTM_PhieuChi", Khoa);
           
        }

        public void NewTM_PhieuChiViewModel()
        {
            BeginRefresh("NewTM_PhieuChi");
            
        }

        public void GetTM_PhieuChiViewModel(int Khoa)
        {
            BeginRefresh("GetTM_PhieuChi", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewTM_PhieuChiViewModel();
        }

        public void SavePhieuChi()
        {
            BeginSave();
            
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.TM_PhieuChi_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TM_PhieuChi_ChiTietChild>>(TM_PhieuChi_ChiTiets_AddedNew);
                Model.TM_PhieuChi_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TM_PhieuChi_ThueChild>>(TM_PhieuChi_Thues_AddedNew);
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.HangHoaIdChanged += new EventHandler<HangHoaIdChangedEventArgs>(OnHangHoaIdChanged);               
                Model.LoaiTienTeChanged+= new EventHandler(OnLoaiTienTeChanged);                
                Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
              
                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }
        #endregion
        
        #region Add TM_PhieuChi Details Item
        public void AddTM_PhieuChi_ChiTietItem()
        {
            Model.TM_PhieuChi_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddTM_PhieuChi_ThueItem()
        {
            Model.TM_PhieuChi_Thues.AddNewItem(Model.Khoa);
        }



        void TM_PhieuChi_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TM_PhieuChi_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TM_PhieuChi_ChiTiets"));
        }      

        void TM_PhieuChi_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TM_PhieuChi_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TM_PhieuChi_Thues"));
        }

        
        


        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

       
        public void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            if (hangHoaIdChanged != null)
            {
                hangHoaIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }
        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        

      

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }

        }

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
        }

      
        #endregion

        #region Delete TM_PhieuChi Details Item

        public void DeleteTM_PhieuChi_ChiTietItem(int Index)
        {
            Model.TM_PhieuChi_ChiTiets.DeleteItem(Index);
        }

        public void DeleteTM_PhieuChi_ThueItem(int Index)
        {
            Model.TM_PhieuChi_Thues.DeleteItem(Index);
        }
       
      
        

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_TM_PhieuChi;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetTM_PhieuChi", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
